package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.market.sys.common.Result;
import com.market.sys.entity.User;
import com.market.sys.service.IUserService;
import com.market.sys.util.Constants;
import com.market.sys.util.EmptyUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

import static com.market.sys.util.Constants.DOWNLOAD_URL;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/wxUser")
public class UserController {

    @Resource
    private IUserService userService;

    @PostMapping("/register")
    public Result<?> register(@RequestBody Map<String,Object> registerData){
        if ("男".equals(String.valueOf(registerData.get("sex")))){
            registerData.put("sex", 0);
        } else if ("女".equals(String.valueOf(registerData.get("sex")))) {
            registerData.put("sex",1);
        }else {
            // 默认是男
            registerData.put("sex", 0);
        }

        // 用BeanUtil将Map转为User实体类
        User user = BeanUtil.fillBeanWithMap(registerData, new User(), false);

        // 判断字段是否为有值
        if (BeanUtil.isEmpty(user)){
            return Result.error("信息为空，注册失败");
        }

        if (EmptyUtil.isStringEmpty(user.getAccount())){
            return Result.error("请输入账号");
        }

        if (EmptyUtil.isStringEmpty(user.getPassword())){
            return Result.error("请输入密码");
        }

        if (EmptyUtil.isStringEmpty(user.getNickName())){
            return Result.error("请输入昵称");
        }

        if (user.getSex() == null){
            return Result.error("请输入性别");
        }

        if (EmptyUtil.isStringEmpty(user.getPhone())){
            return Result.error("请输入联系电话");
        }

        if (EmptyUtil.isStringEmpty(user.getIdentityCard())){
            return Result.error("请输入身份证号");
        }

        if (BeanUtil.isNotEmpty(userService.query().eq("account",user.getAccount()).one())){
            return Result.error("该账号已经有人注册了");
        }

        if (userService.save(user)){
            return Result.success(userService.query().eq("account",user.getAccount()).one(),"注册成功");
        }else {
            return Result.error("注册失败");
        }
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody Map<String,Object> loginData){
        // 用BeanUtil将Map转为User实体类
        User user = BeanUtil.fillBeanWithMap(loginData, new User(), false);

        if (EmptyUtil.isStringEmpty(user.getAccount())){
            return Result.error("请输入账号");
        }

        if (EmptyUtil.isStringEmpty(user.getPassword())){
            return Result.error("请输入密码");
        }

        User loginUser = userService.query().eq("account", user.getAccount())
                .eq("password",user.getPassword()).one();
        if (BeanUtil.isNotEmpty(loginUser)){
            return Result.success(loginUser,"登录成功");
        }else {
            return Result.error("账号密码错误");
        }
    }


    @PostMapping("/updateInfo")
    public Result<?> updateInfo(@RequestBody Map<String,Object> paramsMap){
        User user = BeanUtil.fillBeanWithMap(paramsMap, new User(), false);
        // 调用 MyBatis-Plus 的更新方法进行更新操作
        boolean result = userService.updateById(user);

        if(result){
            User dataUser = userService.query().eq("id", user.getId()).one();
            dataUser.setAvatar(DOWNLOAD_URL + dataUser.getAvatar());
            return Result.success(dataUser,"更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @PostMapping("/updateAccountPassword")
    public Result<?> updateAccountPassword(@RequestBody Map<String,Object> paramsMap){
        User user = BeanUtil.fillBeanWithMap(paramsMap, new User(), false);
        // 调用 MyBatis-Plus 的更新方法进行更新操作
        boolean result = userService.update().set("account",user.getAccount()).set("password",user.getPassword())
                .eq("id",user.getId()).update();

        if(result){
            User dataUser = userService.query().eq("id", user.getId()).one();
            dataUser.setAvatar(DOWNLOAD_URL + dataUser.getAvatar());
            return Result.success(dataUser,"更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @PostMapping("/queryUserById")
    public Result<?> queryUserById(@RequestBody Map<String,Object> paramsMap){
        User user = BeanUtil.fillBeanWithMap(paramsMap, new User(), false);
        user = userService.query().eq("id",user.getId()).one();
        if (!EmptyUtil.isStringEmpty(user.getAvatar())){
            user.setAvatar(DOWNLOAD_URL + user.getAvatar());
        }else {
            user.setAvatar("/assets/images/logo.png");
        }
        return Result.success(user);
    }
}
